var config = {
    paths: { // 如果某个前缀的依赖不是按照baseUrl拼接这么简单，就需要在这里指出
        //zepto: 'http://cdn.bootcss.com/zepto/1.1.6/zepto.min',
        jquery: 'http://cdn.bootcss.com/zepto/1.1.6/zepto.min',
        //jquery: 'http://cdn.bootcss.com/jquery/1.12.1/jquery.min',会默认加载
        underscore: 'http://cdn.bootcss.com/underscore.js/1.8.3/underscore',
        localstorage: 'http://www.css88.com/doc/backbone/examples/backbone.localStorage',
        backbone: 'http://cdn.bootcss.com/backbone.js/1.2.3/backbone-min'
    },
    shim: { // 引入没有使用requirejs模块写法的类库。backbone依赖underscore
        'underscore': {
            exports: '_'
        },
        'jquery': {
            exports: '$'
        },
        'backbone': {
            deps: ['underscore', 'jquery'],
            exports: 'Backbone'
        }
    }
};
require.config(config);

require(["backbone", "jquery","localstorage"], function () {

    //创建一个Todo的对象
    var TodoModel = Backbone.Model.extend({
        // 设置默认的属性,初始值
        defaults: function(){
            var myDate=new Date()
            return {

                //标题
                title: "",
                //排序
                order: todos.nextOrder(),

                //是否已经做过
                done: false,
                //创建时间
                createtime:new Date(),
                //提醒时间
                todotime:new Date()
            }
        }

    });
    // 定义Todo Collection
    var TodoCollection = Backbone.Collection.extend({
        model: TodoModel,
        // 保存在本地
        localStorage: new Backbone.LocalStorage("TodoCollection"),


        //取得顺序。从1开始，恣纵
        nextOrder: function() {
            if (this.length==0){
                return 1;
            } else {
                return this.last().get('order') + 1;
            }
        },
        //指定排序字段
        comparator: 'order'

    });

    var todos = new TodoCollection;

    //定义所有任务view
    var AlltodoView = Backbone.View.extend({
        el: $('#alltt'),
        render: function () {

        },
        events: {
            "click #addnew": "addnew"
        },
        initialize:function(){

        },

        addnew:function(){
            pop.show();
        }


    });
    //个人任务
    var TodoView = Backbone.View.extend({
        //tagName:  "li",
        template: _.template($('#persontodo-template').html()),

        events:{
            "click span.title1"   : "theclick",
            "click span.closeX"  : "closeX"
        },

        closeX:function(){
            this.$el.hide();
            this.model.destroy();
        },
        theclick:function(){
        },
        render: function () {
            this.$el.html(this.template(this .model.toJSON()));
            return this;
        },
        initialize:function(){

        }


    });

    //定义所有任务view
    var AppView = Backbone.View.extend({
        el: $('#todoapp'),
        initialize: function () {
            this.listenTo(todos,"add",this.add);
            this.listenTo(todos, 'reset', this.addAll);
            this.listenTo(todos,"all",this.render);
            //恢复本地存储的数据
            todos.fetch();
        },
        render:function(){
            $("#todonum").html(todos.length);
        },

        add:function(todo){
            var todoView = new TodoView({model:todo});
            $("#todos").append(todoView.render().el);
        },
        addAll: function() {
            todos.each(this.add, this);
        }

    });

    //弹出层view
    var PopupView = Backbone.View.extend({
        el: $('#popDiv'),
        events: {
            "click #add": "add",
            "click #btn_close": "hide"
        },
        add:function(){
            var todo0 = new TodoModel;
            var title = this.title.val();
            if(title==null||title.length<1){
                alert("不能为空");
                return false;
            }

            todo0.set('title',this.title.val());
            todos.add(todo0);
            todo0.save();
            this.hide();
        },
        initialize:function(){
            this.title = $("#newTitle");
        },

        show:function(){
            $('#popDiv').show();
            $('#bg').show();
        },
        hide:function(){
            $('#popDiv').hide();
            $('#bg').hide();
        }
    });


    var app = new AppView;
    var alltodo =  new AlltodoView;
    var pop = new PopupView;
    //var todo1 = new TodoModel;
    //todos.add(todo1);
    //todos.each(function (todo) {
    //    todo.save({'done': true}); }
    //);

})